package com.stu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.stu.commons.ServerResponce;
import com.stu.mapper.CouponMapper;
import com.stu.mapper.MycouponMapper;
import com.stu.pojo.Coupon;
import com.stu.service.ICouponService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;


@Service
public class CouponServiceImpl implements ICouponService {
    @Autowired
    private CouponMapper couponMapper;
    @Autowired
    private MycouponMapper mycouponMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Override
    public Coupon getById(Integer couponId) {
        return couponMapper.selectById(couponId);
    }

    @Override
    public ServerResponce insertCoupon(Coupon coupon) {
        int insert = couponMapper.insert(coupon);
        if(insert>0){
            return ServerResponce.success("添加优惠券成功",coupon);
        }else {
            return ServerResponce.fail("添加优惠券失败",null);
        }

    }
    /*            将优惠卷放进Redis          */
    @Override
    public Map<String, Object> selectByIdCoupon(Integer couponId) {
        String key="Coupon_"+couponId;
        System.out.println("key:"+key);
//        判断Redis中是否有key
        boolean hasKey = redisTemplate.hasKey(key);
        System.out.println("Redis中是否有haskey:"+hasKey);
        ValueOperations valueOperations = redisTemplate.opsForValue();
        Map<String, Object> map = new HashMap<>();
        if(hasKey){
            Integer count =(Integer) valueOperations.get(key);
            System.out.println("从缓存中拿到优惠卷的数量："+count);

            map.put("key",key);
            map.put("count",count);
            return map;
        }else {
//            若没有则从数据库中查询
            Coupon coupon = couponMapper.selectById(couponId);
//            然后再写入缓存当中
            valueOperations.set(key,coupon.getCouponCounts(),5, TimeUnit.HOURS);
            map.put("count",coupon.getCouponCounts());
            return map;
        }
    }
    /*            管理员修改优惠卷库存数       */
    @Override
    public ServerResponce reduceCoupon(Integer couponId, Integer count) {
        Coupon coupon = couponMapper.selectById(couponId);
        coupon.setCouponCounts(count);
        System.out.println("数据库中的优惠卷："+coupon.getCouponCounts());
        return ServerResponce.success("修改成功",null);
    }
    /*            线程获取锁之后获取优惠券添加用户自己的优惠券页面    用户ID 优惠券ID */

    /*            页面渲热优惠券              */
//    @Override
//    public List<Coupon> getAll() {
//        QueryWrapper<Coupon> wrapper = new QueryWrapper<>();
//        wrapper.select("coupon_id","coupon_price","coupon_start_time","coupon_end_time","coupon_rule");
//        List<Coupon> coupons = couponMapper.selectList(wrapper);
//        return coupons;
//    }

    // 改进版,将优惠卷放进Redis
    @Override
    public List<Coupon> getAll() {
        QueryWrapper<Coupon> wrapper = new QueryWrapper<>();
        wrapper.select("coupon_id","coupon_price","coupon_start_time","coupon_end_time","coupon_rule", "coupon_counts");
        List<Coupon> coupons = couponMapper.selectList(wrapper);

        ValueOperations<String, Integer> valueOperations = redisTemplate.opsForValue();
        for (Coupon coupon : coupons) {
            String key = "Coupon_" + coupon.getCouponId();
            if (!redisTemplate.hasKey(key)) {
                valueOperations.set(key, coupon.getCouponCounts(), 5, TimeUnit.HOURS);
            }
        }

        return coupons;
    }

    /*             数据库同步减库存,用户抢优惠券,同时优惠券数量-1              */
    @Override
    public Integer updataCoupon(Coupon coupon) {
//        获得优惠卷version   couponStatus---数据库修改为--->version
        // 懒得改了,就当是版本号吧
        Integer couponStatus = coupon.getCouponStatus();
//        获得优惠券的库存
        Integer couponCounts = coupon.getCouponCounts();
        if(couponCounts>0){
            coupon.setCouponCounts(couponStatus+1);
            coupon.setCouponCounts(couponCounts-1);
            QueryWrapper<Coupon> wrapper = new QueryWrapper<>();
            wrapper.eq("coupon_status",couponStatus);
            wrapper.eq("coupon_counts",couponCounts);
            int i1 = couponMapper.update(coupon, wrapper);

            return i1;
        }else {
            return -1;
        }
    }
}
